<?php

namespace app\index\controller;

use think\Controller;

use app\index\model\Member;

class Register extends Controller
{
    //注册页面
    public function index(){
        return view('/register');
    }

    
    /**
     * 注册会员
     * 
     * @return string  
     */
    public function store(){
        $info = input('post.');

        $result = $this->validate($info, 'app\index\validate\Register');
        if($result !== true) return json($result);

        $isEx = Member::where('memb_phone', $info['phone'])->find();
        if(!empty($isEx)){
            $result = [
                'msg' => '当前手机号已注册',
                'code' => 1,
            ];
        }elseif($info['captcha'] != session('captcha')){
            $result = [
                'msg' => '手机验证码不正确',
                'code' => 1,
            ];
        }else{
            session('captcha', null);
            $member = new Member;
            $member->memb_name      = $info['name'];
            $member->memb_IDCard    = $info['id_card'];
            $member->memb_phone     = $info['phone'];
            $member->memb_password  = password_hash($info['password'], PASSWORD_DEFAULT);
            $member->memb_headImage = "/static/imgs/avatar.png";
            $member->memb_address   = "";
            $member->memb_black     = false;
            $member->memb_point     = 0;
            $member->memb_balance   = 0;
            $member->save();
            $config = db('config')->where('name', 'givemoney')->find();
            if($config['status']){
                Member::updateBalance($member->memb_id, $config['value'], '注册赠送');
            }
            $result = [
                'code' => 0,
                'msg' => '注册成功',
            ];
        }
        return json($result);
    }
}
